<!DOCTYPE html>
<html lang="zh-Hans">
<head>

    <!--[if lt IE 9]>
        <style>body {display: none; background: none !important} </style>
        <meta http-equiv="Refresh" Content="0; url=//outdatedbrowser.com/" />
    <![endif]-->

<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge, chrome=1" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
<meta name="format-detection" content="telephone=no" />
<meta name="author" content="FreeShow" />



<meta property="og:type" content="article">
<meta property="og:title" content="Java之多线程编程">
<meta property="og:url" content="https://freeshow.github.io/Programming/Java/Java之多线程编程/index.html">
<meta property="og:site_name" content="FreeShow">
<meta property="og:updated_time" content="2017-03-28T08:06:31.082Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Java之多线程编程">

<link rel="apple-touch-icon" href= "/apple-touch-icon.png">


    <link rel="alternate" href="/atom.xml" title="FreeShow" type="application/atom+xml">



    <link rel="shortcut icon" href="/favicon.ico">



    <link href="//cdn.bootcss.com/animate.css/3.5.1/animate.min.css" rel="stylesheet">



    <link href="//cdn.bootcss.com/fancybox/2.1.5/jquery.fancybox.min.css" rel="stylesheet">



    <script src="//cdn.bootcss.com/pace/1.0.2/pace.min.js"></script>
    <link href="//cdn.bootcss.com/pace/1.0.2/themes/blue/pace-theme-minimal.css" rel="stylesheet">


<link rel="stylesheet" href="/css/style.css">



<link href="//cdn.bootcss.com/font-awesome/4.6.3/css/font-awesome.min.css" rel="stylesheet">


<title>Java之多线程编程 | FreeShow</title>

<script src="//cdn.bootcss.com/jquery/2.2.4/jquery.min.js"></script>
<script src="//cdn.bootcss.com/clipboard.js/1.5.10/clipboard.min.js"></script>

<script>
    var yiliaConfig = {
        fancybox: true,
        animate: true,
        isHome: false,
        isPost: true,
        isArchive: false,
        isTag: false,
        isCategory: false,
        fancybox_js: "//cdn.bootcss.com/fancybox/2.1.5/jquery.fancybox.min.js",
        scrollreveal: "//cdn.bootcss.com/scrollReveal.js/3.1.4/scrollreveal.min.js",
        search: true
    }
</script>


    <script>
        yiliaConfig.jquery_ui = [true, "//cdn.bootcss.com/jqueryui/1.10.4/jquery-ui.min.js", "//cdn.bootcss.com/jqueryui/1.10.4/css/jquery-ui.min.css"];
    </script>



    <script> yiliaConfig.rootUrl = "\/";</script>






</head>
<body>
  <div id="container">
    <div class="left-col">
    <div class="overlay"></div>
<div class="intrude-less">
    <header id="header" class="inner">
        <a href="/" class="profilepic">
            <img src="/img/avatar.png" class="animated zoomIn">
        </a>
        <hgroup>
          <h1 class="header-author"><a href="/">FreeShow</a></h1>
        </hgroup>

        
        <p class="header-subtitle">在追求艺术的道路上狂奔</p>
        

        
            <form id="search-form">
            <input type="text" id="local-search-input" name="q" placeholder="search..." class="search form-control" autocomplete="off" autocorrect="off" searchonload="false" />
            <i class="fa fa-times" onclick="resetSearch()"></i>
            </form>
            <div id="local-search-result"></div>
            <p class='no-result'>No results found <i class='fa fa-spinner fa-pulse'></i></p>
        


        
            <div id="switch-btn" class="switch-btn">
                <div class="icon">
                    <div class="icon-ctn">
                        <div class="icon-wrap icon-house" data-idx="0">
                            <div class="birdhouse"></div>
                            <div class="birdhouse_holes"></div>
                        </div>
                        <div class="icon-wrap icon-ribbon hide" data-idx="1">
                            <div class="ribbon"></div>
                        </div>
                        
                        <div class="icon-wrap icon-link hide" data-idx="2">
                            <div class="loopback_l"></div>
                            <div class="loopback_r"></div>
                        </div>
                        
                        
                        <div class="icon-wrap icon-me hide" data-idx="3">
                            <div class="user"></div>
                            <div class="shoulder"></div>
                        </div>
                        
                    </div>
                    
                </div>
                <div class="tips-box hide">
                    <div class="tips-arrow"></div>
                    <ul class="tips-inner">
                        <li>菜单</li>
                        <li>标签</li>
                        
                        <li>友情链接</li>
                        
                        
                        <li>关于我</li>
                        
                    </ul>
                </div>
            </div>
        

        <div id="switch-area" class="switch-area">
            <div class="switch-wrap">
                <section class="switch-part switch-part1">
                    <nav class="header-menu">
                        <ul>
                        
                            <li><a href="/">主页</a></li>
                        
                            <li><a href="/archives/">所有文章</a></li>
                        
                            <li><a href="/tags/">标签云</a></li>
                        
                            <li><a href="/about/">关于我</a></li>
                        
                        </ul>
                    </nav>
                    <nav class="header-nav">
                        <ul class="social">
                            
                                <a class="fa Email" href="mailto:877646746@qq.com" title="Email"></a>
                            
                                <a class="fa GitHub" href="http://github.com/freeshow" title="GitHub"></a>
                            
                                <a class="fa 新浪微博" href="http://weibo.com/freeshowself" title="新浪微博"></a>
                            
                                <a class="fa CSDN" href="http://blog.csdn.net/u011026329" title="CSDN"></a>
                            
                        </ul>
                    </nav>
                </section>
                
                
                <section class="switch-part switch-part2">
                    <div class="widget tagcloud" id="js-tagcloud">
                        <ul class="tag-list"><li class="tag-list-item"><a class="tag-list-link" href="/tags/Android/">Android</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/C/">C</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Crawler/">Crawler</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Hadoop/">Hadoop</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Hexo/">Hexo</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Java/">Java</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Linux/">Linux</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Python/">Python</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/SIP/">SIP</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Scala/">Scala</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Shell/">Shell</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Spark/">Spark</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Tools/">Tools</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/WebRTC/">WebRTC</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/XMPP/">XMPP</a></li></ul>
                    </div>
                </section>
                
                
                
                <section class="switch-part switch-part3">
                    <div id="js-friends">
                    
                      <a class="main-nav-link switch-friends-link" href="https://hexo.io">Hexo</a>
                    
                      <a class="main-nav-link switch-friends-link" href="https://github.com/freeshow">GitHub</a>
                    
                      <a class="main-nav-link switch-friends-link" href="http://freeshow.github.io/">FreeShow</a>
                    
                    </div>
                </section>
                

                
                
                <section class="switch-part switch-part4">
                
                    <div id="js-aboutme">我是一个喜欢无拘无束、追求自由、热爱分享的小小小码农！</div>
                </section>
                
            </div>
        </div>
    </header>                
</div>
    </div>
    <div class="mid-col">
      <nav id="mobile-nav">
      <div class="overlay">
          <div class="slider-trigger"></div>
          <h1 class="header-author js-mobile-header hide"><a href="/" title="回到主页">FreeShow</a></h1>
      </div>
    <div class="intrude-less">
        <header id="header" class="inner">
            <a href="/" class="profilepic">
                <img src="/img/avatar.png" class="animated zoomIn">
            </a>
            <hgroup>
              <h1 class="header-author"><a href="/" title="回到主页">FreeShow</a></h1>
            </hgroup>
            
            <p class="header-subtitle">在追求艺术的道路上狂奔</p>
            
            <nav class="header-menu">
                <ul>
                
                    <li><a href="/">主页</a></li>
                
                    <li><a href="/archives/">所有文章</a></li>
                
                    <li><a href="/tags/">标签云</a></li>
                
                    <li><a href="/about/">关于我</a></li>
                
                <div class="clearfix"></div>
                </ul>
            </nav>
            <nav class="header-nav">
                        <ul class="social">
                            
                                <a class="fa Email" target="_blank" href="mailto:877646746@qq.com" title="Email"></a>
                            
                                <a class="fa GitHub" target="_blank" href="http://github.com/freeshow" title="GitHub"></a>
                            
                                <a class="fa 新浪微博" target="_blank" href="http://weibo.com/freeshowself" title="新浪微博"></a>
                            
                                <a class="fa CSDN" target="_blank" href="http://blog.csdn.net/u011026329" title="CSDN"></a>
                            
                        </ul>
            </nav>
        </header>                
    </div>
    <link class="menu-list" tags="标签" friends="友情链接" about="关于我"/>
</nav>
      <div class="body-wrap"><article id="post-Programming/Java/Java之多线程编程" class="article article-type-post" itemscope itemprop="blogPost">
  
    <div class="article-meta">
      <a href="/Programming/Java/Java之多线程编程/" class="article-date">
      <time datetime="2016-07-23T15:12:43.000Z" itemprop="datePublished">2016-07-23</time>
</a>


    </div>
  
  <div class="article-inner">
    
      <input type="hidden" class="isFancy" />
    
    
      <header class="article-header">
        
  
    <h1 class="article-title" itemprop="name">
      Java之多线程编程
    </h1>
  

      </header>
      
      <div class="article-info article-info-post">
        
    <div class="article-category tagcloud">
    <a class="article-category-link" href="/categories/编程语言/">编程语言</a>
    </div>


        
    <div class="article-tag tagcloud">
        <ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/Java/">Java</a></li></ul>
    </div>

        <div class="clearfix"></div>
      </div>
      
    
    <div class="article-entry" itemprop="articleBody">
      
          
        <p><excerpt in="" index="" |="" 首页摘要=""><br><a id="more"></a></excerpt></p>
<the rest="" of="" contents="" |="" 余下全文="">

<pre><code>在单核系统中，并发执行的线程共享CPU，每个线程得到一片时间周期。所以，在单核系统中，两个或更多个线程不是真正的同时运行，但是
</code></pre><p>空闲时间被利用了。然而，在多核系统中，两个或更多个线程可能是真正同步执行。在许多情况下，这会进一步提高程序的效率并提高特定操作的速度。</p>
<p><strong>11.1.1 线程优先级</strong></p>
<pre><code>Java为每个线程都指定了优先级，优先级决定了相对于其他线程应当如何处理某个线程。线程的优先级用于决定何时从一个运行的线程切换到写一个。这种称为上下文切换(context switch)。决定上下文切换发生时机的规则比较简单：
</code></pre><p>(1)线程自愿的放弃控制。线程显示的放弃控制、休眠或在I/O之前阻塞，都会出现这种情况。在这种情况下，检查所有其他线程，并且准备运行的线程中优先级高的那个线程会获取CPU资源。</p>
<p>(2)线程被允许优先级更高的线程取代。对于这种情况，没有放弃控制权的低优先级线程不管正在做什么，都会被高优先级线程取代。基本上，只要高优先级 的线程希望运行，它就会取代低优先级的线程，这种称为抢占式多任务处理(preemptive multitasking)。 </p>
<p>   如果具有相同优先级的两个线程竞争CPU资源，这种情况有些复杂。对于Windows这类操作系统，优先级相等的线程以循环方式自动获取CPU资源。对于其他操作系统，优先级相等的线程必须自愿的向其他线程放弃控制权，否则其他线程就不能运行。</p>
<p><strong>11.1.2 同步</strong></p>
<pre><code>此处讲得同步其实是操作系统中的互斥。
例如，链表，就需要以某种方式确保它们之间不会发生冲突。也就是说，当一个线程正在读取该数据结构时，必须阻止另一个线程向该数据结构写入数据。
为此，Java以监视器这一年代久远的进程间同步模型为基础，实现了一种巧妙的方案。监视器最初由C.A.R.Hoare定义的一种控制机制，可以将监视器看作非常小的只能包含一个线程的盒子。一旦某个线程进入监视器，其他所有线程必须等待，直到该线程退出监视器。通过这种方式，可以将监视器用于保护共享的资源，以防止多个线程同时对资源进程操作。

Java没有提供&quot;Monitor&quot;类；相反，每个对象都有自己隐式的监视器。如果调用对象的同步方法，就会自动进入对象的隐式监视器。一旦某个线程位于一个同步方法中，其他线程就不能调用同一对象的任何其他同步方法。因为语言本身内置了同步支持，所以可以编写出非常清晰并且简明的多线程代码。
</code></pre><p><strong>11.1.3 消息传递</strong></p>
<pre><code>将程序分隔到独立的线程之后，需要定义它们之间相互通信的方式。当使用某些其他语言编程时，必须依赖操作系统建立线程之间的通信。当然，这会增加系统开销。相反，通过调用所有对象都具有的预定义的方法，Java为两个或更多个线程之间的相互通信提供了一种简洁的低成本方法。Java的消息传递系统允许某个线程进入对象的同步方法，然后进行等待，直到其他线程显示地通知这个线程退出为止。
</code></pre><p><strong>11.1.4</strong></p>
<pre><code>Java的多线程系统是基于Thread类、Thread类的方法及其伴随接口Runnable而构建的。Thread类封装了线程的执行。因为不能直接引用正在运行的线程的细微状态，所以需要通过代理进行处理，Thread实例就是线程的代理。为了创建新线程，程序可以扩展Thread类或实现Runnable接口。
Thread类定义了一些用于帮助管理线程的方法。
getName():获取线程的名称。
getPriority()：获取线程的优先级。
isAlive()：确定线程是否仍然运行。
join():等待线程终止。
run():线程的入口点。
sleep():挂起线程一段时间。
start():通过调用线程的run()方法启动线程。
</code></pre><p><strong>11.2 主线程</strong></p>
<pre><code>当Java程序启动时，会立即开始运行一个线程，因为它是程序开始时执行的线程，所以这个线程通常称为程序的主线程。主线程很重要，有以下两个原因：
(1)其他线程都是从主线程产生的。
(2)通常，主线程必须是最后才结束执行的线程，因为它要执行各种关闭动作。

尽管主线程是在程序启动时自动创建的，但是可以通过Thread对象对其进行控制。为此，必须调用currentThread()方法获取对主线程的一个引用。该方法是Thread类的公有静态成员，它的一般形式如下：
static Thread currentThread()
这个方法返回对调用它的线程的引用。一旦得到对主线程的引用，就可以像控制其他线程那样控制主线程了。

sleep()方法使线程从调用时挂起，暂缓执行指定的时间间隔(毫秒数),它的一般形式如下：
static void sleep(long milliseconds) throws InterruptedException
sleep()方法可能会抛出InterruptedException异常。如果其他线程试图中断这个正在睡眠的线程，就会抛出这个异常。
</code></pre><p><strong>11.3 创建线程</strong></p>
<pre><code>在最通常情况下，通过实例化Thread类型的对象创建线程。Java定义了创建线程的两种方法：
(1)实现Runnable接口 (重载Runnalbe接口中的run()方法)
(2)扩展Thread类本身 (需要从Java.lang.Thread类派生一个新的线程类，重载它的run()方法)
</code></pre><p><strong>11.3.1 实现Runnable接口</strong></p>
<pre><code>创建线程最简单的方式是创建实现了Runnable接口的类。Runnable接口抽象了一个可执行代码单元。可以依托任何实现了Runnable接口的对象来创建线程。
</code></pre><p>为了实现Runnable接口，类只需要实现run()方法，该方法的声明如下：</p>
<blockquote>
<pre><code>public void run()
</code></pre></blockquote>
<pre><code>在run()方法内部，定义组成新线程的代码。run()方法可以调用其他方法，使用其他类，可也以声明变量，就像main线程那样，理解这一点很重要。唯一的区别是：run()方法为程序中另外一个并发线程的执行建立了入口点。当run()方法返回时，这个线程将结束。

在创建实现了Runnable接口的类之后，可以在类中实例化Thread类的对象。Thread类定义了几个构造函数。我们使用的那个构造函数如下所示：

Thread(Runnable threadOb,String threadName)

在这个构造函数中，threadOb是实现了Runnable接口的类的实例，这定义了从何开始执行线程。新线程的名字有threadName指定。

 在创建了新线程之后，只有调用线程的start()方法，线程才会运行，该方法是在Thread类中声明的。本质上，start()方法执行对run()方法的调用。
 start()方法的声明如下所示：

 void start()

 例如：
</code></pre><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div><div class="line">53</div><div class="line">54</div><div class="line">55</div></pre></td><td class="code"><pre><div class="line">class NewThread implements Runnable</div><div class="line">&#123;</div><div class="line">Thread t;</div><div class="line"></div><div class="line">NewThread()</div><div class="line">&#123;</div><div class="line">	//Create a new thread.</div><div class="line">	t = new Thread(this,&quot;Demo Thread&quot;);</div><div class="line">	System.out.println(&quot;Child thread: &quot;+t);</div><div class="line">	//Start thread </div><div class="line">	t.start();</div><div class="line">&#125;</div><div class="line"></div><div class="line">public void run()</div><div class="line">&#123;</div><div class="line">	try</div><div class="line">	&#123;</div><div class="line">		for(int i = 5; i&gt;0; i--)</div><div class="line">		&#123;</div><div class="line">			System.out.println(&quot;Child Thread: &quot;+i);</div><div class="line">			Thread.sleep(500);</div><div class="line">		&#125;</div><div class="line">		</div><div class="line">	&#125;</div><div class="line">	catch(InterruptedException e)</div><div class="line">	&#123;</div><div class="line">		System.out.println(&quot;Child interrupted.&quot;);</div><div class="line">	&#125;</div><div class="line">		</div><div class="line">	System.out.println(&quot;Exiting child thread.&quot;);</div><div class="line">&#125;	</div><div class="line">&#125;</div><div class="line"></div><div class="line">class ThreadDemo </div><div class="line">&#123;</div><div class="line">public static void main(String[] args)</div><div class="line">&#123;</div><div class="line">	new NewThread();</div><div class="line">	</div><div class="line">	try</div><div class="line">	&#123;</div><div class="line">		for(int i = 5; i&gt;0; i--)</div><div class="line">		&#123;</div><div class="line">			System.out.println(&quot;Main Thread: &quot;+i);</div><div class="line">			Thread.sleep(1000);</div><div class="line">		&#125;</div><div class="line">	&#125;</div><div class="line">	catch(InterruptedException e)</div><div class="line">	&#123;</div><div class="line">		System.out.println(&quot;Main Thread interrupted.&quot;);</div><div class="line">	&#125;</div><div class="line">	</div><div class="line">	System.out.println(&quot;Main thread exiting.&quot;);</div><div class="line">&#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<pre><code>在NewThread类的构造函数中，通过下面这条语句创建了一个新的Thread对象:

t = new Thread(this,&quot;Demo Thread&quot;);

传递this作为第一个参数，以表明希望新线程调用this对象的run()方法。接下来调用start()方法，从run()方法开始启动线程的执行。这会导致开始执行子线程的for循环。调用完start()方法之后，NewThread类的构造函数返回到main()方法。当恢复主线程时，会主线程的for循环。这两个线程继续运行，在单核系统中它们会共享CPU，直到它们的循环结束。

另外提供一种，比较符合人们编程方式的上面代码的书写方式，即不在实现了Runnable接口的类中创建/启动线程。
</code></pre><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div></pre></td><td class="code"><pre><div class="line">class NewThread implements Runnable</div><div class="line"> &#123;</div><div class="line"> </div><div class="line">	public void run()</div><div class="line">	&#123;</div><div class="line">		try</div><div class="line">		&#123;</div><div class="line">			for(int i = 5; i&gt;0; i--)</div><div class="line">			&#123;</div><div class="line">				System.out.println(&quot;Child Thread: &quot;+i);</div><div class="line">				Thread.sleep(500);</div><div class="line">			&#125;</div><div class="line">			</div><div class="line">		&#125;</div><div class="line">		catch(InterruptedException e)</div><div class="line">		&#123;</div><div class="line">			System.out.println(&quot;Child interrupted.&quot;);</div><div class="line">		&#125;</div><div class="line">			</div><div class="line">		System.out.println(&quot;Exiting child thread.&quot;);</div><div class="line">	&#125;	</div><div class="line"> &#125;</div><div class="line"> </div><div class="line"> class ThreadDemo </div><div class="line"> &#123;</div><div class="line">	public static void main(String[] args)</div><div class="line">	&#123;</div><div class="line">		Runnable r = new NewThread();</div><div class="line">		Thread t = new Thread(r,&quot;Demo Thread&quot;);</div><div class="line">		t.start();</div><div class="line">		</div><div class="line">		try</div><div class="line">		&#123;</div><div class="line">			for(int i = 5; i&gt;0; i--)</div><div class="line">			&#123;</div><div class="line">				System.out.println(&quot;Main Thread: &quot;+i);</div><div class="line">				Thread.sleep(1000);</div><div class="line">			&#125;</div><div class="line">		&#125;</div><div class="line">		catch(InterruptedException e)</div><div class="line">		&#123;</div><div class="line">			System.out.println(&quot;Main Thread interrupted.&quot;);</div><div class="line">		&#125;</div><div class="line">		</div><div class="line">		System.out.println(&quot;Main thread exiting.&quot;);</div><div class="line">	&#125;</div><div class="line"> &#125;</div></pre></td></tr></table></figure>
<p><strong>13.2 扩展Thread类</strong></p>
<pre><code>扩展类必须重写run()方法，run()方法是新线程的入口点。扩展类还必须调用start()方法以开始新线程的执行。
使用扩展Thread类对上面程序就行就该：
</code></pre><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div><div class="line">53</div><div class="line">54</div><div class="line">55</div><div class="line">56</div></pre></td><td class="code"><pre><div class="line">class NewThread extends Thread</div><div class="line">&#123;</div><div class="line">	NewThread()</div><div class="line">	&#123;</div><div class="line">		super(&quot;Demo Thread&quot;);</div><div class="line">		System.out.println(&quot;Child thread: &quot;+this);</div><div class="line">		start(); //start the thread </div><div class="line">	&#125;</div><div class="line">	</div><div class="line">	public void run()</div><div class="line">	&#123;</div><div class="line">		try</div><div class="line">		&#123;</div><div class="line">			for(int i=5; i&gt;0; i--)</div><div class="line">			&#123;</div><div class="line">				System.out.println(&quot;Child Thread: &quot;+i);</div><div class="line">				Thread.sleep(500);</div><div class="line">			&#125;</div><div class="line">		&#125;</div><div class="line">		catch(InterruptedException e)</div><div class="line">		&#123;</div><div class="line">			System.out.println(&quot;Child interrupted.&quot;);</div><div class="line">		&#125;</div><div class="line">		</div><div class="line">		System.out.println(&quot;Exiting child thread.&quot;);</div><div class="line">	&#125;</div><div class="line">&#125;</div><div class="line"></div><div class="line">class ExtendThread</div><div class="line">&#123;</div><div class="line">	public static void main(String[] args)</div><div class="line">	&#123;</div><div class="line">		new NewThread();</div><div class="line">		</div><div class="line">		/**</div><div class="line">		*	当NewThread类中的构造函数中没有start()方法时，可以这样书写，比较符合一般人的习惯。</div><div class="line">		*	Thread t = new NewThread();</div><div class="line">		*	t.start()</div><div class="line">		*/</div><div class="line">		</div><div class="line">		try</div><div class="line">		&#123;</div><div class="line">			for(int i = 5; i&gt;0; i--)</div><div class="line">			&#123;</div><div class="line">				System.out.println(&quot;Main Thread: &quot;+i);</div><div class="line">				Thread.sleep(1000);</div><div class="line">			&#125;</div><div class="line">		&#125;</div><div class="line">		catch(InterruptedException e)</div><div class="line">		&#123;</div><div class="line">			System.out.println(&quot;Main Thread interrupted.&quot;);</div><div class="line">		&#125;</div><div class="line">		</div><div class="line">		System.out.println(&quot;Main thread exiting.&quot;);</div><div class="line">	&#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p><strong>11.3.3 选择一种创建方式</strong>    </p>
<pre><code>为什么Java要提供两种方法来创建线程呢？它们都有哪些区别？相比而言，哪一种方法更好呢？
在Java中，类仅支持单继承，也就是说，当定义一个新的类的时候，它只能扩展一个外部类.这样，如果创建自定义线程类的时候是通过扩展 Thread类的方法来实现的，那么这个自定义类就不能再去扩展其他的类，也就无法实现更加复杂的功能。因此，如果自定义类必须扩展其他的类，那么就可以使用实现Runnable接口的方法来定义该类为线程类，这样就可以避免Java单继承所带来的局限性。

还有一点最重要的就是使用实现Runnable接口的方式创建的线程可以处理同一资源，从而实现资源的共享.

假设一个影院有三个售票口，分别用于向儿童、成人和老人售票。影院为每个窗口放有100张电影票，分别是儿童票、成人票和老人票。三个窗口需要同时卖票，而现在只有一个售票员，这个售票员就相当于一个CPU，三个窗口就相当于三个线程。通过程序来看一看是如何创建这三个线程的。

（1）通过扩展Thread类来创建多线程
</code></pre><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div></pre></td><td class="code"><pre><div class="line">class MutliThread extends Thread</div><div class="line">&#123;</div><div class="line">	private int ticket=100;//每个线程都拥有100张票</div><div class="line"></div><div class="line">		MutliThread(String name)</div><div class="line">		&#123;</div><div class="line">			super(name);//调用父类带参数的构造方法</div><div class="line">		&#125;</div><div class="line"></div><div class="line">		public void run()</div><div class="line">		&#123;</div><div class="line">			while(ticket&gt;0)</div><div class="line">			&#123;</div><div class="line">				System.out.println(ticket--+&quot; is saled by &quot;+Thread.currentThread().getName());</div><div class="line">				ticket--;</div><div class="line">			&#125;</div><div class="line">		&#125;</div><div class="line">&#125;</div><div class="line"></div><div class="line">public class MutliThreadDemo </div><div class="line">&#123;</div><div class="line">	public static void main(String [] args)</div><div class="line">	&#123;</div><div class="line">		MutliThread m1=new MutliThread(&quot;Window 1&quot;);</div><div class="line">		MutliThread m2=new MutliThread(&quot;Window 2&quot;);</div><div class="line">		MutliThread m3=new MutliThread(&quot;Window 3&quot;);</div><div class="line">		</div><div class="line">		m1.start();</div><div class="line">		m2.start();</div><div class="line">		m3.start();</div><div class="line">	&#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<pre><code>程序中定义一个线程类，它扩展了Thread类。利用扩展的线程类在MutliThreadDemo类的主方法中创建了三个线程对象，并通过start()方法分别将它们启动。

从结果可以看到，每个线程分别对应100张电影票，之间并无任何关系，这就说明每个线程之间是平等的，没有优先级关系，因此都有机会得到CPU的处理。但是结果显示这三个线程并不是依次交替执行，而是在三个线程同时被执行的情况下，有的线程被分配时间片的机会多，票被提前卖完，而有的线程被分配时间片的机会比较少，票迟一些卖完。

可见，利用扩展Thread类创建的多个线程，虽然执行的是相同的代码，但彼此相互独立，且各自拥有自己的资源，互不干扰。
</code></pre><p>   （2）通过实现Runnable接口来创建多线程</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div></pre></td><td class="code"><pre><div class="line">class MutliThread implements Runnable</div><div class="line">&#123;</div><div class="line">	private int ticket=100;//每个线程都拥有100张票</div><div class="line">	private String name;</div><div class="line">	</div><div class="line">	MutliThread(String name)</div><div class="line">	&#123;</div><div class="line">		this.name=name;</div><div class="line">	&#125;</div><div class="line">	</div><div class="line">	public void run()</div><div class="line">	&#123;</div><div class="line">		while(ticket&gt;0)</div><div class="line">		&#123;</div><div class="line">			System.out.println(ticket--+&quot; is saled by &quot;+name);</div><div class="line">			ticket--;</div><div class="line">		&#125;</div><div class="line">	&#125;</div><div class="line">&#125;</div><div class="line"></div><div class="line">public class MutliThreadDemo2 </div><div class="line">&#123;</div><div class="line">	public static void main(String [] args)</div><div class="line">	&#123;</div><div class="line">		MutliThread m1=new MutliThread(&quot;Window 1&quot;);</div><div class="line">		MutliThread m2=new MutliThread(&quot;Window 2&quot;);</div><div class="line">		MutliThread m3=new MutliThread(&quot;Window 3&quot;);</div><div class="line"></div><div class="line">		Thread t1=new Thread(m1);</div><div class="line">		Thread t2=new Thread(m2);</div><div class="line">		Thread t3=new Thread(m3);</div><div class="line"></div><div class="line">		t1.start();</div><div class="line">		t2.start();</div><div class="line">		t3.start();</div><div class="line">	&#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<pre><code>由于这三个线程也是彼此独立，各自拥有自己的资源，即100张电影票，因此程序输出的结果和（1）结果大同小异。均是各自线程对自己的100张票进行单独的处理，互不影响。
可见，只要现实的情况要求保证新建线程彼此相互独立，各自拥有资源，且互不干扰，采用哪个方式来创建多线程都是可以的。因为这两种方式创建的多线程程序能够实现相同的功能。
</code></pre><p>   （3）通过实现Runnable接口来实现线程间的资源共享</p>
<p>   现实中也存在这样的情况，比如模拟一个火车站的售票系统，假如当日从A地发往B地的火车票只有100张，且允许所有窗口卖这100张票，那么每一个窗口也相当于一个线程，但是这时和前面的例子不同之处就在于所有线程处理的资源是同一个资源，即100张车票。如果还用前面的方式来创建线程显然是无法实现的，这种情况该怎样处理呢？看下面这个程序，程序代码如下所示：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div></pre></td><td class="code"><pre><div class="line">class MutliThread implements Runnable</div><div class="line">&#123;</div><div class="line">	private int ticket=100;//每个线程都拥有100张票</div><div class="line">	</div><div class="line">	public void run()</div><div class="line">	&#123;</div><div class="line">		while(ticket&gt;0)</div><div class="line">		&#123;</div><div class="line">			System.out.println(ticket--+&quot; is saled by &quot;+name);</div><div class="line">			ticket--;</div><div class="line">		&#125;</div><div class="line">	&#125;</div><div class="line">&#125;</div><div class="line"></div><div class="line">public class MutliThreadDemo2 </div><div class="line">&#123;</div><div class="line">	public static void main(String [] args)</div><div class="line">	&#123;</div><div class="line">		MutliThread m=new MutliThread();</div><div class="line"></div><div class="line">		Thread t1=new Thread(m,&quot;Window 1&quot;);</div><div class="line">		Thread t2=new Thread(m,&quot;Window 2&quot;);</div><div class="line">		Thread t3=new Thread(m,&quot;Window 3&quot;);</div><div class="line"></div><div class="line">		t1.start();</div><div class="line">		t2.start();</div><div class="line">		t3.start();</div><div class="line">	&#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<pre><code>结果正如前面分析的那样，程序在内存中仅创建了一个资源，而新建的三个线程都是基于访问这同一资源的，并且由于每个线程上所运行的是相同的代码，因此它们执行的功能也是相同的。

可见，如果现实问题中要求必须创建多个线程来执行同一任务，而且这多个线程之间还将共享同一个资源，那么就可以使用实现Runnable接口的方式来创建多线程程序。而这一功能通过扩展Thread类是无法实现的，读者想想看，为什么？
答案：通过扩展Thread类可以实现，只需将ticket设置成静态static类型，读写的时候进行加锁即可。但是，实现ruanable接口的这种方式，明显是定义一个类，创建一个对象，然后跟着创建三个线程，他们共享该对象的成员变量。相比第一种继承Thread，要创建三个对象，然后创建三个线程，最后按照你们的方法创建一个static的变量，再加锁，这个方式，并不是很好。

实现Runnable接口相对于扩展Thread类来说，具有无可比拟的优势。这种方式不仅有利于程序的健壮性，使代码能够被多个线程共享，而且代码和数据资源相对独立，从而特别适合多个具有相同代码的线程去处理同一资源的情况。这样一来，线程、代码和数据资源三者有效分离，很好地体现了面向对象程序设计的思想。因此，几乎所有的多线程程序都是通过实现Runnable接口的方式来完成的。
</code></pre><p><strong>11.5 使用isAlive()和join()方法</strong> </p>
<pre><code>前面提到，通常希望主线程在最后结束。在前面的例子中通过sleep()方法，并指定足够长的延迟时间来确保所有子线程在主线程之前终止。但是，这完全不是一个令人满意的方案，并且还能造成一个更大的问题：线程如何知道另一个线程何时结束？幸运的是，Thread类提供了一种能够解决这个问题的方法。

有两种方法可以确实线程是否已经结束。
首先，可以为线程调用isAlive()方法。这个方法是由Thread类定义的，它的一般形式如下所示：

final boolean isAlive()

如果线程仍在运行，isAlive()方法就会返回true,否则返回false.

虽然isAlive()方法很有用，但是通常使用join()方法来等待线程结束，如下所示：

final void join() throws InterruptedException

该方法一直等待，直到调用线程终止。如此命名该方法的原因是：调用线程一直等待，直到指定的线程加入(join)其中为止。join()方法的另外一种形式，允许指定希望等待指定线程终止的最长时间。

下面是前面例子的改进版本，该版本使用join()方法确保主线程在最后结束，另外演示了isAlive()方法的使用：
</code></pre><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div><div class="line">53</div><div class="line">54</div><div class="line">55</div><div class="line">56</div><div class="line">57</div><div class="line">58</div><div class="line">59</div><div class="line">60</div><div class="line">61</div><div class="line">62</div><div class="line">63</div><div class="line">64</div></pre></td><td class="code"><pre><div class="line">class NewThread implements Runnable</div><div class="line">&#123;</div><div class="line">	String name; //name of thread</div><div class="line">	Thread t;</div><div class="line">	</div><div class="line">	NewThread(String threadName)</div><div class="line">	&#123;</div><div class="line">		name = threadName;</div><div class="line">		t = new NewThread(this,name);</div><div class="line">		System.out.println(&quot;New Thread: &quot;+t);</div><div class="line">		t.start();</div><div class="line">	&#125;</div><div class="line">	</div><div class="line">	public void run()</div><div class="line">	&#123;</div><div class="line">		try</div><div class="line">		&#123;</div><div class="line">			for(int i=5; i&gt;0; i--)</div><div class="line">			&#123;</div><div class="line">				System.out.println(name+&quot;: &quot;+i);</div><div class="line">				Thread.sleep(1000);</div><div class="line">			&#125;</div><div class="line">			catch(InterruptedException e)</div><div class="line">			&#123;</div><div class="line">				System.out.println(&quot;name&quot;+&quot; interrupted&quot;);</div><div class="line">			&#125;</div><div class="line">			</div><div class="line">			System.out.println(name+&quot; exiting&quot;);</div><div class="line">		&#125;</div><div class="line">	&#125;</div><div class="line">&#125;</div><div class="line"></div><div class="line">class DemoJoin</div><div class="line">&#123;</div><div class="line">	public static void main(String[] args)</div><div class="line">	&#123;</div><div class="line">		NewThread ob1 = new NewThread(&quot;One&quot;);</div><div class="line">		NewThread ob2 = new NewThread(&quot;Two&quot;);</div><div class="line">		NewThread ob3 = new NewThread(&quot;Three&quot;);</div><div class="line">		</div><div class="line">		System.out.println(&quot;Thread One is alive: &quot;+ob1.t.isAlive());</div><div class="line">		System.out.println(&quot;Thread Two is alive: &quot;+ob2.t.isAlive());</div><div class="line">		System.out.println(&quot;Thread Three is alive: &quot;+ob3.t.isAlive());</div><div class="line">		</div><div class="line">		//waiting for threads to finish</div><div class="line">		try</div><div class="line">		&#123;</div><div class="line">			System.out.println(&quot;Waiting for threads to finish.&quot;);</div><div class="line">			ob1.t.join();</div><div class="line">			ob2.t.join();</div><div class="line">			ob3.t.join();</div><div class="line">		&#125;</div><div class="line">		catch(InterruptedException e)</div><div class="line">		&#123;</div><div class="line">			System.out.println(&quot;Main Thread interrupted&quot;);</div><div class="line">		&#125;</div><div class="line">		</div><div class="line">		System.out.println(&quot;Thread One is alive: &quot;+ob1.t.isAlive());</div><div class="line">		System.out.println(&quot;Thread Two is alive: &quot;+ob2.t.isAlive());</div><div class="line">		System.out.println(&quot;Thread Three is alive: &quot;+ob3.t.isAlive());</div><div class="line">		</div><div class="line">		System.out.println(&quot;Main Thread exiting&quot;);</div><div class="line">	&#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<pre><code>可以看出，在join()方法调用返回之后，线程停止执行。
</code></pre><p><strong>11.6 线程优先级</strong> </p>
<pre><code>为了设置线程的优先级，需要使用setPriority()方法，它是Thread类的成员，下面是该方法的一般形式：

final void setPriority(int level)

其中，level指定了为调用线程设置的新优先级，默认level == 5.level的值必须在MIN_PRIORITY和MAX_PRIORITY之间选择。目前，这些值分别是1和10.
</code></pre><p>如果希望线程设置为默认优先级，可以使用NORM_PRIORITY，目前的值是5.这些优先级在Thread类中作为static final变量定义的。</p>
<pre><code>可以通过调用Thread类的getPriority()方法获取当前的优先级，该方法如下所示：

final int getPriority()
</code></pre><p><strong>11.7 同步</strong> </p>
<pre><code>当两个或多个线程需要访问共享资源时，它们需要以某种方式确保每一次只有一个线程使用资源。实现这一目的的过程称为同步。

同步的关键是监视器的概念，前面已经叙述。监视器是用作互斥锁的对象。在给定时刻，只有一个线程可以拥有监视器。

可以使用两种方法同步代码。这两种方法都要用到synchronized关键字，下面分别介绍这两种方法。
</code></pre><p><strong>11.7.1  使用同步方法</strong></p>
<pre><code>在Java中进行同步很容易，因为所有对象都有与他们自身关联的隐式的监视器。为了进入对象的监视器，只需要调用synchronized关键字修饰过
</code></pre><p>的方法。当某个线程进入同步方法中，调用同一实例的该同步方法(或任何其他同步方法)的所有其他线程都必须等待。为了退出监视器并将对象的控制权<br>交给下一个等待线程，监视器的拥有者只需要简单的从同步方法中返回。</p>
<pre><code>下面介绍一个应当使用但是没有使用同步的例子：
</code></pre><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div><div class="line">53</div><div class="line">54</div><div class="line">55</div><div class="line">56</div><div class="line">57</div><div class="line">58</div><div class="line">59</div><div class="line">60</div><div class="line">61</div><div class="line">62</div><div class="line">63</div><div class="line">64</div></pre></td><td class="code"><pre><div class="line">//This program is not synchronized</div><div class="line"> </div><div class="line"> class Callme</div><div class="line"> &#123;</div><div class="line">	void call(String msg)</div><div class="line">	&#123;</div><div class="line">		System.out.println(&quot;[&quot;+msg);</div><div class="line">		</div><div class="line">		try</div><div class="line">		&#123;</div><div class="line">			Thread.sleep(1000);</div><div class="line">		&#125;</div><div class="line">		catch(InterruptedException e)</div><div class="line">		&#123;</div><div class="line">			System.out.println(&quot;Interrupted&quot;);</div><div class="line">		&#125;</div><div class="line">		</div><div class="line">		System.out.println(&quot;]&quot;)</div><div class="line">	&#125;</div><div class="line"> &#125;</div><div class="line"> </div><div class="line"> class Caller implements Runnable</div><div class="line"> &#123;</div><div class="line">	String msg;</div><div class="line">	Callme target;</div><div class="line">	Thread t;</div><div class="line">	</div><div class="line">	Caller(Callme targ,String s)</div><div class="line">	&#123;</div><div class="line">		target = targ;</div><div class="line">		msg = s;</div><div class="line">		t = new Thread(this);</div><div class="line">		t.start();</div><div class="line">	&#125;</div><div class="line">	</div><div class="line">	void run()</div><div class="line">	&#123;</div><div class="line">		target.call(msg);</div><div class="line">	&#125;</div><div class="line"> &#125;</div><div class="line"> </div><div class="line"> class Synch</div><div class="line"> &#123;</div><div class="line">	public static void main(String[] args)</div><div class="line">	&#123;</div><div class="line">		Callme target = new Callme();</div><div class="line">		</div><div class="line">		Caller th1 = new Caller(target,&quot;Hello&quot;);</div><div class="line">		Caller th2 = new Caller(target,&quot;Java&quot;);</div><div class="line">		Caller th3 = new Caller(target,&quot;Android&quot;);</div><div class="line">		</div><div class="line">		//waiting for threads to end</div><div class="line">		try</div><div class="line">		&#123;</div><div class="line">			th1.t.join();</div><div class="line">			th2.t.join();</div><div class="line">			th3.t.join();</div><div class="line">		&#125;</div><div class="line">		catch(InterruptedException e)</div><div class="line">		&#123;</div><div class="line">			System.out.println(&quot;Interrupted&quot;);</div><div class="line">		&#125;</div><div class="line">	&#125;</div><div class="line"> &#125;</div></pre></td></tr></table></figure>
<pre><code> 下面该程序生成的输出：
 Hello[Java[Android]
 ]
 ]

 可以看出，通过调用sleep()方法，call()方法允许执行切换到另一个线程，这会导致混合输出3个消息字符串。在这个程序中，没有采取什么方法以阻止3个线程在相同的时间调用同一对象的同一方法，这就是所谓的竞态条件(race condition),因为3个线程相互竞争以完成方法。这个例子中使用了sleep()方法，使得效果可以重复并且十分明显。在大多数情况下，竞态条件会更加微妙并且不可预测，因为不能确定何时会发生线程上下文切换。这会造成程序在某一次运行正确，而在下一次运行错误。

为了修复前面的程序，必须按顺序调用call()方法。也就是说，必须限制每次只能由一个线程调用call()方法。为此，只需要简单的在call方法定义的前面添加关键字synchronized，如下所示：
</code></pre><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">class Callme</div><div class="line">&#123;</div><div class="line">	synchronized void call(String msg)</div><div class="line">	&#123;&#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<pre><code>当一个线程使用call()方法时，这会阻止其他线程进入该方法。将synchronized关键字添加到call()方法之后，程序输出如下：
[Hello]
[Java]
[Android]

在多线程情况下，如果有一个或一组方法来操作对象的内部状态(对象中的数据)，那么每次都应当使用synchronized关键字，以保证状态不会进入竞态条件。
</code></pre><p>请记住，一旦线程进入了一个实例的同步方法，所有其他线程就都不能再进入相同实例的任何同步方法。但是，仍然可以继续调用同一实例的非同步部分。</p>
<p><strong>11.7.2 synchronized语句</strong></p>
<pre><code>虽然在类中创建同步方法是一种比较容易并且行之有效的实现同步的方式，但并不是在所有情况下都可以使用这种方式。为了理解其中的原因，我们分析下面的内容。假设某个类没有针对多线程访问进行设计，即类没有使用同步方法，而又希望同步对类的访问。进一步讲，类不是由你创建的，而是由第三方创建的，并且你不能访问类的源代码。因此，不能为类中的合适方法添加synchronized修饰符。如何同步访问这种类的对象呢？幸运的是，这个问题的解决方案很容易：可以简单的将对这种类定义的方法的调用放到synchronized代码块中。

下面是synchronized语句的一般形式：
</code></pre><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">synchronized(objRef)</div><div class="line">&#123;</div><div class="line">	//statements to be synchronized</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<pre><code>其中，objRef是对被同步对象的引用。synchronized代码块确保对objRef对象的成员方法的调用，只会在当前线程成功进入objRef的监视器之后发生。

下面是前面例子的另一个版本，该版本在run()方法中使用synchronized代码块：
</code></pre><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div><div class="line">53</div><div class="line">54</div><div class="line">55</div><div class="line">56</div><div class="line">57</div><div class="line">58</div><div class="line">59</div><div class="line">60</div><div class="line">61</div><div class="line">62</div><div class="line">63</div><div class="line">64</div><div class="line">65</div><div class="line">66</div><div class="line">67</div><div class="line">68</div></pre></td><td class="code"><pre><div class="line">//This program uses a synchronized block</div><div class="line"></div><div class="line">class Callme</div><div class="line"> &#123;</div><div class="line">	void call(String msg)</div><div class="line">	&#123;</div><div class="line">		System.out.println(&quot;[&quot;+msg);</div><div class="line">		</div><div class="line">		try</div><div class="line">		&#123;</div><div class="line">			Thread.sleep(1000);</div><div class="line">		&#125;</div><div class="line">		catch(InterruptedException e)</div><div class="line">		&#123;</div><div class="line">			System.out.println(&quot;Interrupted&quot;);</div><div class="line">		&#125;</div><div class="line">		</div><div class="line">		System.out.println(&quot;]&quot;)</div><div class="line">	&#125;</div><div class="line"> &#125;</div><div class="line"> </div><div class="line"> class Caller implements Runnable</div><div class="line"> &#123;</div><div class="line">	String msg;</div><div class="line">	Callme target;</div><div class="line">	Thread t;</div><div class="line">	</div><div class="line">	Caller(Callme targ,String s)</div><div class="line">	&#123;</div><div class="line">		target = targ;</div><div class="line">		msg = s;</div><div class="line">		t = new Thread(this);</div><div class="line">		t.start();</div><div class="line">	&#125;</div><div class="line">	</div><div class="line">	void run()</div><div class="line">	&#123;</div><div class="line">		//uses synchronized block</div><div class="line">		synchronized(target)</div><div class="line">		&#123;</div><div class="line">			target.call(msg);</div><div class="line">		&#125;</div><div class="line">	&#125;</div><div class="line"> &#125;</div><div class="line"> </div><div class="line"> class Synch</div><div class="line"> &#123;</div><div class="line">	public static void main(String[] args)</div><div class="line">	&#123;</div><div class="line">		Callme target = new Callme();</div><div class="line">		</div><div class="line">		Caller th1 = new Caller(target,&quot;Hello&quot;);</div><div class="line">		Caller th2 = new Caller(target,&quot;Java&quot;);</div><div class="line">		Caller th3 = new Caller(target,&quot;Android&quot;);</div><div class="line">		</div><div class="line">		//waiting for threads to end</div><div class="line">		try</div><div class="line">		&#123;</div><div class="line">			th1.t.join();</div><div class="line">			th2.t.join();</div><div class="line">			th3.t.join();</div><div class="line">		&#125;</div><div class="line">		catch(InterruptedException e)</div><div class="line">		&#123;</div><div class="line">			System.out.println(&quot;Interrupted&quot;);</div><div class="line">		&#125;</div><div class="line">	&#125;</div><div class="line"> &#125;</div></pre></td></tr></table></figure>
<pre><code>在此，没有使用synchronized修饰call()方法。反而在Caller类的run()方法中使用了synchronized语句，这会使该版本的输出和前面版本的相同。
</code></pre><p><strong>11.8 线程间通信</strong></p>
<pre><code>这里的线程间通信其实操作系统中所讲的同步，同步首先互斥。例如，生产者和消费者问题。

Java通过提供wait()、notify()以及notifyAll()方法，提供了一种巧妙的进程间通信机制，这些方法在Object中是作为final方法实现的，因此所有类都具有这些方法。所有这3个方法都只能在同步上下文中调用：
(1)wait()方法通知调用线程放弃监视器并进入休眠，直到其他一些线程进入同一个监视器并调用notify()方法或notifyAll()方法。 
(2)notify()方法唤醒调用相同对象的wait()方法的线程。
(3)notifyAll()方法唤醒调用相同对象的wait()方法的所有线程，其中的一个线程将得到访问授权。

这些方法都是在Object类中定义的，如下所示：

final void wait() throws InterruptedException
final void notify()
final void notifyAll()

wait()方法还有梁歪一种形式，允许指定等待的时间间隔。

在通过例子演示线程通信之前，还有重要的一点需要指出。尽管在正常情况下，wait()方法会等待直到调用notify()或notifyAll()方法，但是还有一种几率很小却可能发生的情况，等待线程由于假唤醒(spurious wakeup)而被唤醒。对于这种情况，等待线程也会被唤醒。然而却没有调用notify()或notifyAll()方法(本质上，线程在没有什么明显理由的情况下就被恢复了)。因为存在这种极小却可能发生的情况，Orical推荐应当在一个检查线程等待条件的循环中调用wait()方法。

首先分析下面的示例程序，该例以不正确的方式实现了一个简单形式的生产者/消费者问题。该例包含4个类：类Q是试图同步的队列；类Producer是产生队列条目的线程对象；类Customer是使用队列条目的线程对象；类PC是一个小类型，用于创建类Q、Producer和Customer的实例。
</code></pre><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div><div class="line">53</div><div class="line">54</div><div class="line">55</div><div class="line">56</div><div class="line">57</div><div class="line">58</div><div class="line">59</div><div class="line">60</div><div class="line">61</div><div class="line">62</div><div class="line">63</div><div class="line">64</div><div class="line">65</div><div class="line">66</div><div class="line">67</div><div class="line">68</div></pre></td><td class="code"><pre><div class="line">class Q</div><div class="line">&#123;</div><div class="line">	int n;</div><div class="line">	</div><div class="line">	synchronized int get()</div><div class="line">	&#123;</div><div class="line">		System.out.println(&quot;Got: &quot;+n);</div><div class="line">		return n;</div><div class="line">	&#125;</div><div class="line">	</div><div class="line">	synchronized void put(int n)</div><div class="line">	&#123;</div><div class="line">		this.n = n;</div><div class="line">		System.out.println(&quot;Put: &quot;+n);</div><div class="line">	&#125;</div><div class="line">&#125;</div><div class="line"></div><div class="line">class Producter implements Runnable</div><div class="line">&#123;</div><div class="line">	Q q;</div><div class="line">	</div><div class="line">	Producter(Q q)</div><div class="line">	&#123;</div><div class="line">		this.q = q;</div><div class="line">		new Thread(this,&quot;Producter&quot;).start();</div><div class="line">	&#125;</div><div class="line">	</div><div class="line">	public void run()</div><div class="line">	&#123;</div><div class="line">		int i = 0;</div><div class="line">		</div><div class="line">		while(true)</div><div class="line">		&#123;</div><div class="line">			q.put(++i);</div><div class="line">		&#125;</div><div class="line">	&#125;</div><div class="line">&#125;</div><div class="line"></div><div class="line">class Consumer implements Runnable</div><div class="line">&#123;</div><div class="line">	Q q;</div><div class="line">	</div><div class="line">	Consumer(Q q)</div><div class="line">	&#123;</div><div class="line">		this.q = q;</div><div class="line">		new Thread(this,&quot;Consumer&quot;).start();</div><div class="line">	&#125;</div><div class="line">	</div><div class="line">	public void run()</div><div class="line">	&#123;</div><div class="line">		while(true)</div><div class="line">		&#123;</div><div class="line">			q.get();</div><div class="line">		&#125;</div><div class="line">	&#125;</div><div class="line">&#125;</div><div class="line"></div><div class="line">class PC</div><div class="line">&#123;</div><div class="line">	public static void main(String[] args)</div><div class="line">	&#123;</div><div class="line">		Q q = new Q();</div><div class="line">		new Producter(q);</div><div class="line">		new Consumer(q);</div><div class="line">		</div><div class="line">		System.out.println(&quot;Press Contrlo-C to stop.&quot;);</div><div class="line">	&#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<pre><code>输出如下(根据处理器的速度和加载任务，实际输出可能不同)：

put: 1 
get: 1 
get: 1 
get: 1 
get: 1 
get: 1 
put: 2 
put: 3
put: 4
put: 5 
put: 6
put: 7
get: 7
可以看出，生产者在将1放入队列之后，消费者开始运行，并且连续5次获得相同的数值1.然后，生产者恢复执行，并产生数值2到7，而不让消费者有机会使用它们。

使用Java编写这个程序的正确方式是使用wain()和notify()方法在两个方向上发信号，如下所示：
</code></pre><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div><div class="line">53</div><div class="line">54</div><div class="line">55</div><div class="line">56</div><div class="line">57</div><div class="line">58</div><div class="line">59</div><div class="line">60</div><div class="line">61</div><div class="line">62</div><div class="line">63</div><div class="line">64</div><div class="line">65</div><div class="line">66</div><div class="line">67</div><div class="line">68</div><div class="line">69</div><div class="line">70</div><div class="line">71</div><div class="line">72</div><div class="line">73</div><div class="line">74</div><div class="line">75</div><div class="line">76</div><div class="line">77</div><div class="line">78</div><div class="line">79</div><div class="line">80</div><div class="line">81</div><div class="line">82</div><div class="line">83</div><div class="line">84</div><div class="line">85</div><div class="line">86</div><div class="line">87</div><div class="line">88</div><div class="line">89</div><div class="line">90</div><div class="line">91</div><div class="line">92</div><div class="line">93</div><div class="line">94</div><div class="line">95</div><div class="line">96</div><div class="line">97</div><div class="line">98</div><div class="line">99</div></pre></td><td class="code"><pre><div class="line">class Q</div><div class="line">&#123;</div><div class="line">	int n;</div><div class="line">	//刚开始valueSet == 0,表示队列为空，没有数据。生产者可以向队列中放入数据，将valueSet = true,表示队列中存入了数据，消费者可以从</div><div class="line">	//队列中取出数据。取出数据后valueSet = 0，生产者可以放入数据，依次循环。</div><div class="line">	boolean valueSet = false;</div><div class="line">	</div><div class="line">	synchronized int get()</div><div class="line">	&#123;</div><div class="line">		while(!valueSet)</div><div class="line">		&#123;</div><div class="line">			try</div><div class="line">			&#123;</div><div class="line">				wait();</div><div class="line">			&#125;</div><div class="line">			catch(InterruptedException)</div><div class="line">			&#123;</div><div class="line">				System.out.println(&quot;InterruptedException caught.&quot;);</div><div class="line">			&#125;</div><div class="line">		&#125;</div><div class="line">		</div><div class="line">		System.out.println(&quot;Got: &quot;+n);</div><div class="line">		valueSet = false;</div><div class="line">		notify();</div><div class="line">		return n;</div><div class="line">	&#125;</div><div class="line">	</div><div class="line">	synchronized void put(int n)</div><div class="line">	&#123;</div><div class="line">		while(valueSet)</div><div class="line">		&#123;</div><div class="line">			try</div><div class="line">			&#123;</div><div class="line">				wait();</div><div class="line">			&#125;</div><div class="line">			catch(InterruptedException e)</div><div class="line">			&#123;</div><div class="line">				System.out.println(&quot;InterruptedException caught.&quot;);</div><div class="line">			&#125;</div><div class="line">		&#125;</div><div class="line">		</div><div class="line">		this.n = n;</div><div class="line">		valueSet = true;</div><div class="line">		System.out.println(&quot;Put: &quot;+n);</div><div class="line">		notify();</div><div class="line">	&#125;</div><div class="line">&#125;</div><div class="line"></div><div class="line">class Producter implements Runnable</div><div class="line">&#123;</div><div class="line">	Q q;</div><div class="line">	</div><div class="line">	Producter(Q q)</div><div class="line">	&#123;</div><div class="line">		this.q = q;</div><div class="line">		new Thread(this,&quot;Producter&quot;).start();</div><div class="line">	&#125;</div><div class="line">	</div><div class="line">	public void run()</div><div class="line">	&#123;</div><div class="line">		int i = 0;</div><div class="line">		</div><div class="line">		while(true)</div><div class="line">		&#123;</div><div class="line">			q.put(++i);</div><div class="line">		&#125;</div><div class="line">	&#125;</div><div class="line">&#125;</div><div class="line"></div><div class="line">class Consumer implements Runnable</div><div class="line">&#123;</div><div class="line">	Q q;</div><div class="line">	</div><div class="line">	Consumer(Q q)</div><div class="line">	&#123;</div><div class="line">		this.q = q;</div><div class="line">		new Thread(this,&quot;Consumer&quot;).start();</div><div class="line">	&#125;</div><div class="line">	</div><div class="line">	public void run()</div><div class="line">	&#123;</div><div class="line">		while(true)</div><div class="line">		&#123;</div><div class="line">			q.get();</div><div class="line">		&#125;</div><div class="line">	&#125;</div><div class="line">&#125;</div><div class="line"></div><div class="line">class PC</div><div class="line">&#123;</div><div class="line">	public static void main(String[] args)</div><div class="line">	&#123;</div><div class="line">		Q q = new Q();</div><div class="line">		new Producter(q);</div><div class="line">		new Consumer(q);</div><div class="line">		</div><div class="line">		System.out.println(&quot;Press Contrlo-C to stop.&quot;);</div><div class="line">	&#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<pre><code>输出如下：
put: 1
get: 1 
put: 2 
get: 2 
put: 3 
get: 3 
put: 4 
get: 4 
put: 5 
get: 5
</code></pre><p>死锁：</p>
<pre><code>例如，当两个线程循环依赖一对同步对象时，会发生死锁现象。
假设一个线程进入对象X的监视器，另一个线程进入对象Y的监视器。如果X中的线程试图调用对象Y中的任何同步方法，同时Y中的线程也试图调用X中的任何同步方法，则X中的线程和Y中的线程同时被阻塞，两个线程则会永远等待下去。

例如： 
</code></pre><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div><div class="line">53</div><div class="line">54</div><div class="line">55</div><div class="line">56</div><div class="line">57</div><div class="line">58</div><div class="line">59</div><div class="line">60</div><div class="line">61</div><div class="line">62</div><div class="line">63</div><div class="line">64</div><div class="line">65</div><div class="line">66</div><div class="line">67</div><div class="line">68</div><div class="line">69</div><div class="line">70</div><div class="line">71</div><div class="line">72</div><div class="line">73</div><div class="line">74</div><div class="line">75</div><div class="line">76</div><div class="line">77</div><div class="line">78</div><div class="line">79</div></pre></td><td class="code"><pre><div class="line">class A </div><div class="line">&#123;</div><div class="line">	synchronized void foo(B b)</div><div class="line">	&#123;</div><div class="line">		String name = Thread.currentThread.getName();</div><div class="line">		</div><div class="line">		System.out.println(name+&quot; entered A.foo&quot;);</div><div class="line">		</div><div class="line">		try</div><div class="line">		&#123;</div><div class="line">			Thread.sleep(1000);</div><div class="line">		&#125;</div><div class="line">		catch(InterruptedException e)</div><div class="line">		&#123;</div><div class="line">			System.out.println(&quot;A interrupted&quot;);</div><div class="line">		&#125;</div><div class="line">		</div><div class="line">		System.out.println(name+&quot; trying to call B.last()&quot;);</div><div class="line">		b.last();</div><div class="line">	&#125;</div><div class="line">	</div><div class="line">	synchronized void last()</div><div class="line">	&#123;</div><div class="line">		System.out.println(&quot;Inside A.last&quot;);</div><div class="line">	&#125;</div><div class="line">&#125;</div><div class="line"></div><div class="line">class B </div><div class="line">&#123;</div><div class="line">	synchronized void bar(A a)</div><div class="line">	&#123;</div><div class="line">		String name = Thread.currentThread().getName();</div><div class="line">		System.out.println(name+&quot; entered B.bar&quot;);</div><div class="line">		</div><div class="line">		try</div><div class="line">		&#123;</div><div class="line">			Thread.sleep(1000);</div><div class="line">		&#125;</div><div class="line">		catch(InterruptedException e)</div><div class="line">		&#123;</div><div class="line">			System.out.println(&quot;B Interrupted&quot;);</div><div class="line">		&#125;</div><div class="line">		</div><div class="line">		System.out.println(name+&quot; trying to call A.last()&quot;);</div><div class="line">		a.last();</div><div class="line">	&#125;</div><div class="line">	</div><div class="line">	synchronized void last()</div><div class="line">	&#123;</div><div class="line">		System.out.println(&quot;Inside A.last&quot;);</div><div class="line">	&#125;</div><div class="line">&#125;</div><div class="line"></div><div class="line">class Deadlock implements Runnable</div><div class="line">&#123;</div><div class="line">	A a = new A();</div><div class="line">	B b = new B();</div><div class="line">	</div><div class="line">	Deadlock()</div><div class="line">	&#123;</div><div class="line">		Thread.currentThread().setName(&quot;MainThread&quot;);</div><div class="line">		Thread t = new Thread(this,&quot;RacingThread&quot;);</div><div class="line">		t.start();</div><div class="line">		</div><div class="line">		a.foo(b);</div><div class="line">		System.out.println(&quot;Back in main thread&quot;);</div><div class="line">	&#125;</div><div class="line">	</div><div class="line">	public void run()</div><div class="line">	&#123;</div><div class="line">		b.bar(a);</div><div class="line">		System.out.println(&quot;Back in other thread&quot;);</div><div class="line">	&#125;</div><div class="line">	</div><div class="line">	public static void main(String[] args)</div><div class="line">	&#123;</div><div class="line">		new Deadlock();</div><div class="line">	&#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<pre><code>当运行这个程序，会看到如下输出：
MainThread entered A.foo
RacingThread entered B.bar
MainThread trying to call B.last()
RacingThread trying to call A.last()

可以看出程序出现了死锁线程。当等待a的监视器时，RacingThread拥有b的监视器。同时，MainThread拥有a,并且在等待获取b。这个程序永远不会结束。正如该程序所演示的那样，如果多线程程序偶尔被锁住，那么首先应当检查是否是由于死锁造成的。
</code></pre><p><strong>11.9 挂起、恢复与停止线程</strong></p>
<pre><code>有时挂起线程的执行是有用的。例如，可以使用单独的线程显示一天的时间。如果用户不想要时钟，那么可以挂起时钟线程。无论是什么情况，挂起线程都是一件简单的事情。线程一旦挂起，重新启动线程也很简单。

Java早起版本(例如Java 1.0)和现代版本(例如Java 2.0开始)提供的用来挂起、恢复以及停止线程的机制不同。在Java 2以前，程序使用Thread类定义的suspend()、resume()和stop()方法，暂停、重新启动和停止线程的执行。

虽然这些方法对于管理线程执行看起来是一种合理并且方便的方式，但是在新的Java程序中不能使用它们。下面是其中的原因。在几年前Java 2 不推荐使用Thread类的suspend()方法，因为suspend()方法有时会导致严重的系统故障。假定为关键数据结构加锁，如果这是线程被挂起，那么这些锁将无法释放。其他可能等待这些资源的线程会被死锁。

方法resume()也不推荐使用。虽然不会造成问题，但是如果不使用suspend()方法，就不能使用resume()方法，它们是配对使用的。

对于Thread类的stop()方法，Java 2也反对使用，因为这个方法也会造成严重的系统故障。假定线程正在向关键的重要数据结构中写入数据，并且只完成了部分发生变化的数据。如果这是停止线程，那么数据结构可能会处于损坏状态。问题是：stop()会导致释放调用线程的所有锁。因此，另一正在等待相同锁的线程可能会使用这些以损坏的数据。

在现代Java版本中，线程必须被设计为run()方法周期性的进行检查，以确定是否应当挂起、恢复或停止线程自身的执行。通常这是通过建立用来标志线程执行状态的变量完成的。这要这个标志变量被设置为&quot;运行&quot;，run()方法就必须让线程继续执行。如果标志变量被设置为&quot;挂起&quot;,线程就必须暂停。如果标志变量被设置为&quot;停止&quot;，线程就必须停止。当然，编写这种代码的方式有很多，但是对于所有程序中心主题是相同的。

下面例子演示了如何使用继承自Object的wait()和notify()方法控制线程的执行。下面分析这个程序中的操作。
NewThread类包含布尔型实例变量suspendFlag，该变量用于控制线程的执行，构造函数将该变量初始化为false。方法run()包含检查suspendFlag变量的synchronized代码块。如果该变量为true,就调用wait()方法，挂起线程的执行。mysuspend()方法将suspendFlag变量设置为true。myresume()方法将suspendFlag 设置为false，并调用notify()方法以唤醒线程。最后，对main()方法进行修改以调用myspend()和myresume()方法。 
</code></pre><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div><div class="line">53</div><div class="line">54</div><div class="line">55</div><div class="line">56</div><div class="line">57</div><div class="line">58</div><div class="line">59</div><div class="line">60</div><div class="line">61</div><div class="line">62</div><div class="line">63</div><div class="line">64</div><div class="line">65</div><div class="line">66</div><div class="line">67</div><div class="line">68</div><div class="line">69</div><div class="line">70</div><div class="line">71</div><div class="line">72</div><div class="line">73</div><div class="line">74</div><div class="line">75</div><div class="line">76</div><div class="line">77</div><div class="line">78</div><div class="line">79</div><div class="line">80</div><div class="line">81</div><div class="line">82</div><div class="line">83</div><div class="line">84</div><div class="line">85</div><div class="line">86</div><div class="line">87</div><div class="line">88</div><div class="line">89</div><div class="line">90</div><div class="line">91</div><div class="line">92</div><div class="line">93</div><div class="line">94</div><div class="line">95</div><div class="line">96</div><div class="line">97</div></pre></td><td class="code"><pre><div class="line">class NewThread implements Runnable</div><div class="line">&#123;</div><div class="line">	String name;</div><div class="line">	Thread t;</div><div class="line">	boolean suspendFlag;</div><div class="line">	</div><div class="line">	NewThread(String threadName)</div><div class="line">	&#123;</div><div class="line">		name = threadName;</div><div class="line">		t = new Thread(this,name);</div><div class="line">		System.out.println(&quot;NewThread: &quot;+t);</div><div class="line">		suspendFlag = false;</div><div class="line">		t.start();</div><div class="line">	&#125;</div><div class="line">	</div><div class="line">	public void run()</div><div class="line">	&#123;</div><div class="line">		try</div><div class="line">		&#123;</div><div class="line">			for(int i = 15; i &gt; 0; i--)</div><div class="line">			&#123;</div><div class="line">				System.out.println(name+&quot;: &quot;+i);</div><div class="line">				Thread.sleep(200);</div><div class="line">				</div><div class="line">				synchronized(this)</div><div class="line">				&#123;</div><div class="line">					while(suspendFlag)</div><div class="line">					&#123;</div><div class="line">						wait();</div><div class="line">					&#125;</div><div class="line">				&#125;</div><div class="line">			&#125;</div><div class="line">		&#125;</div><div class="line">		catch(InterruptedException e)</div><div class="line">		&#123;</div><div class="line">			System.out.println(name+&quot; interrupted&quot;);</div><div class="line">		&#125;</div><div class="line">		</div><div class="line">		System.out.println(name+&quot; exiting&quot;);</div><div class="line">	&#125;</div><div class="line">	</div><div class="line">	synchronized void mysuspend()</div><div class="line">	&#123;</div><div class="line">		suspendFlag = true;</div><div class="line">	&#125;</div><div class="line">	</div><div class="line">	synchronized void myresume()</div><div class="line">	&#123;</div><div class="line">		suspendFlag = false;</div><div class="line">		notify();</div><div class="line">	&#125;</div><div class="line">&#125;</div><div class="line"></div><div class="line">class SuspendResume</div><div class="line">&#123;</div><div class="line">	public static void main(String[] args)</div><div class="line">	&#123;</div><div class="line">		NewThread ob1 = new NewThread(&quot;One&quot;);</div><div class="line">		NewThread ob2 = new NewThread(&quot;Two&quot;);</div><div class="line">		</div><div class="line">		try</div><div class="line">		&#123;</div><div class="line">			Thread.sleep(1000);</div><div class="line">			ob1.mysuspend();</div><div class="line">			System.out.println(&quot;Suspending thread One&quot;);</div><div class="line">			</div><div class="line">			Thread.sleep(1000);</div><div class="line">			ob1.myresume();</div><div class="line">			System.out.println(&quot;Resume thread One&quot;);</div><div class="line">			</div><div class="line">			Thread.sleep(1000);</div><div class="line">			ob2.mysuspend();</div><div class="line">			System.out.println(&quot;Suspending thread Two&quot;);</div><div class="line">			</div><div class="line">			Thread.sleep(1000);</div><div class="line">			ob2.myresume();</div><div class="line">			System.out.println(&quot;Resume thread Two&quot;);</div><div class="line">		&#125;</div><div class="line">		catch(InterruptedException e)</div><div class="line">		&#123;</div><div class="line">			System.out.println(&quot;Main Thread Interrupted&quot;);</div><div class="line">		&#125;</div><div class="line">		</div><div class="line">		try</div><div class="line">		&#123;</div><div class="line">			System.out.println(&quot;Waiting for threads to finish&quot;);</div><div class="line">			ob1.t.join();</div><div class="line">			ob2.t.join();</div><div class="line">		&#125;</div><div class="line">		catch(InterruptedException e)</div><div class="line">		&#123;</div><div class="line">			System.out.println(&quot;Main thread exiting&quot;);</div><div class="line">		&#125;</div><div class="line">		</div><div class="line">		System.out.println(&quot;Main thread exiting&quot;);</div><div class="line">	&#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<pre><code>后面会看到更多使用现代线程控制机制的例子。尽管这种机制没有旧机制那么&quot;清晰&quot;,但是不管怎样，这是确保不会发生运行错误所需要做的。
</code></pre><p>对于所有新代码，必须使用这种方式。</p>
<p><strong>11.10 获取线程的状态</strong> </p>
<pre><code>线程可以处于许多不同的状态。可以调用Thread类定义的getState()方法来获取线程的当前状态，该方法如下所示：

Thread.State getState()

该方法返回Thread.State类型的值，指示在调用该方法时线程所处的状态。State是Thread类定义的枚举类型。
</code></pre><p>出自：《Java 8编程参考官方教程(第9版)》</p>
</the>
      
    </div>
    
  </div>
  
    
    <div class="copyright">
        <p><span>本文标题:</span><a href="/Programming/Java/Java之多线程编程/">Java之多线程编程</a></p>
        <p><span>文章作者:</span><a href="/" title="回到主页">FreeShow</a></p>
        <p><span>发布时间:</span>2016-07-23, 23:12:43</p>
        <p><span>最后更新:</span>2017-03-28, 16:06:31</p>
        <p>
            <span>原始链接:</span><a class="post-url" href="/Programming/Java/Java之多线程编程/" title="Java之多线程编程">https://freeshow.github.io/Programming/Java/Java之多线程编程/</a>
            <span class="copy-path" data-clipboard-text="原文: https://freeshow.github.io/Programming/Java/Java之多线程编程/　　作者: FreeShow" title="点击复制文章链接"><i class="fa fa-clipboard"></i></span>
            <script> var clipboard = new Clipboard('.copy-path'); </script>
        </p>
        <p>
            <span>许可协议:</span><i class="fa fa-creative-commons"></i> <a rel="license" href="http://creativecommons.org/licenses/by-nc-sa/4.0/" title="CC BY-NC-SA 4.0 International" target = "_blank">"署名-非商用-相同方式共享 4.0"</a> 转载请保留原文链接及作者。
        </p>
    </div>



    <nav id="article-nav">
        
            <div id="article-nav-newer" class="article-nav-title">
                <a href="/Programming/Java/Java之异常处理/">
                    Java之异常处理
                </a>
            </div>
        
        
            <div id="article-nav-older" class="article-nav-title">
                <a href="/Programming/Java/Java之枚举、自动装箱与注解（元数据）/">
                    Java之枚举、自动装箱与注解（元数据）
                </a>
            </div>
        
    </nav>

  
</article>

    <div id="toc" class="toc-article">
        <strong class="toc-title">文章目录</strong>
        
            
        
    </div>
    <style>
        .left-col .switch-btn,
        .left-col .switch-area {
            display: none;
        }
        .toc-level-3 i,
        .toc-level-3 ol {
            display: none !important;
        }
    </style>

    <input type="button" id="tocButton" value="隐藏目录"  title="点击按钮隐藏或者显示文章目录">

    <script>
        yiliaConfig.toc = ["隐藏目录", "显示目录", !!"false"];
    </script>



    
<div class="share">
    
        <div class="bdsharebuttonbox">
            <a href="#" class="fa fa-twitter bds_twi" data-cmd="twi" title="分享到推特"></a>
            <a href="#" class="fa fa-weibo bds_tsina" data-cmd="tsina" title="分享到新浪微博"></a>
            <a href="#" class="fa fa-qq bds_sqq" data-cmd="sqq" title="分享给 QQ 好友"></a>
            <a href="#" class="fa fa-files-o bds_copy" data-cmd="copy" title="复制网址"></a>
            <a href="#" class="fa fa fa-envelope-o bds_mail" data-cmd="mail" title="通过邮件分享"></a>
            <a href="#" class="fa fa-weixin bds_weixin" data-cmd="weixin" title="生成文章二维码"></a>
            <a href="#" class="fa fa-share-alt bds_more" data-cmd="more"></i></a>
        </div>
        <script>
            window._bd_share_config={
                "common":{"bdSnsKey":{},"bdText":"Java之多线程编程　| FreeShow　","bdMini":"2","bdMiniList":false,"bdPic":"","bdStyle":"0","bdSize":"24"},"share":{}};with(document)0[(getElementsByTagName('head')[0]||body).appendChild(createElement('script')).src='http://bdimg.share.baidu.com/static/api/js/share.js?v=89860593.js?cdnversion='+~(-new Date()/36e5)];
        </script>
    

    
</div>







    
      <div class="duoshuo" id="comments">
    <div id="comment-box" ></div>
    <div class="ds-thread" id="ds-thread" data-thread-key="Programming/Java/Java之多线程编程/" data-title="Java之多线程编程" data-url="https://freeshow.github.io/Programming/Java/Java之多线程编程/"></div>
    <script>
        var duoshuoQuery = {short_name:"freeshowgithub"};
        var loadComment = function(){
            var d = document, s = d.createElement('script');
            s.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') + '//static.duoshuo.com/embed.js';
            s.async = true; s.charset = 'UTF-8';
            (d.head || d.body).appendChild(s);
        }

        
    </script>
    
    <script> loadComment(); </script>

</div>
    




    <div class="scroll" id="post-nav-button">
        
            <a href="/Programming/Java/Java之异常处理/" title="上一篇: Java之异常处理">
                <i class="fa fa-angle-left"></i>
            </a>
        

        <a title="文章列表"><i class="fa fa-bars"></i><i class="fa fa-times"></i></a>

        
            <a href="/Programming/Java/Java之枚举、自动装箱与注解（元数据）/" title="下一篇: Java之枚举、自动装箱与注解（元数据）">
                <i class="fa fa-angle-right"></i>
            </a>
        
    </div>

    <ul class="post-list"><li class="post-list-item"><a class="post-list-link" href="/BigData/Hadoop/Hadoop分布式集群安装/">Hadoop分布式集群安装</a></li><li class="post-list-item"><a class="post-list-link" href="/BigData/Hive/Hive应用实例：WordCount/">Hive应用实例：WordCount</a></li><li class="post-list-item"><a class="post-list-link" href="/BigData/Hive/Hive安装/">Hive安装</a></li><li class="post-list-item"><a class="post-list-link" href="/BigData/Hadoop/Windows下使用eclipse插件运行自己的MapReduce程序/">Windows下使用Eclipse插件运行自己的MapReduce程序</a></li><li class="post-list-item"><a class="post-list-link" href="/BigData/Hadoop/MapReduce之去重计数类应用/">MapReduce之去重计数类应用</a></li><li class="post-list-item"><a class="post-list-link" href="/BigData/Hadoop/MapReduce之简单排序类应用/">MapReduce之简单排序类应用</a></li><li class="post-list-item"><a class="post-list-link" href="/BigData/Hadoop/MapReduce之连接操作类应用/">MapReduce之连接操作类应用</a></li><li class="post-list-item"><a class="post-list-link" href="/BigData/Hadoop/MapReduce之计数类应用/">MapReduce之计数类应用</a></li><li class="post-list-item"><a class="post-list-link" href="/BigData/Hadoop/MapReduce之二次排序类应用/">MapReduce之二次排序类应用</a></li><li class="post-list-item"><a class="post-list-link" href="/BigData/Hadoop/MapReduce之倒排索引类应用/">MapReduce之倒排索引类应用</a></li><li class="post-list-item"><a class="post-list-link" href="/BigData/Hadoop/HDFS Java API/">HDFS Java API</a></li><li class="post-list-item"><a class="post-list-link" href="/BigData/Kafka/Kafka集群安装/">Kafka集群安装</a></li><li class="post-list-item"><a class="post-list-link" href="/BigData/Hadoop/Windows下使用eclipse编译打包运行自己的MapReduce程序 Hadoop2.6.0/">Windows下使用eclipse编译打包运行自己的MapReduce程序 Hadoop2.6.0</a></li><li class="post-list-item"><a class="post-list-link" href="/BigData/ZooKeeper/ZooKeeper集群安装/">ZooKeeper集群安装</a></li><li class="post-list-item"><a class="post-list-link" href="/BigData/Spark/Spark安装和集群部署/">Spark安装和集群部署</a></li><li class="post-list-item"><a class="post-list-link" href="/Comprehensive/BuildBlog/GitHub Pages + Hexo搭建个人博客/">GitHub Pages + Hexo搭建个人博客</a></li><li class="post-list-item"><a class="post-list-link" href="/Programming/Python/Python爬虫实例：登录豆瓣并修改签名/">Python爬虫实例：登录豆瓣并修改签名</a></li><li class="post-list-item"><a class="post-list-link" href="/Programming/Python/Python爬虫实例：用requests重构豆瓣热播电影爬虫/">Python爬虫实例：用requests重构豆瓣热播电影爬虫</a></li><li class="post-list-item"><a class="post-list-link" href="/Programming/Python/Python爬虫实例：豆瓣热播电影/">Python爬虫实例：豆瓣热播电影（urllib+urllib2）</a></li><li class="post-list-item"><a class="post-list-link" href="/Programming/Python/Python爬虫之requests介绍/">Python爬虫之requests介绍</a></li><li class="post-list-item"><a class="post-list-link" href="/Programming/Python/Python爬虫之urllib2介绍/">Python爬虫之urllib2介绍</a></li><li class="post-list-item"><a class="post-list-link" href="/Programming/Python/Python爬虫之urllib介绍/">Python爬虫之urllib介绍</a></li><li class="post-list-item"><a class="post-list-link" href="/Programming/Python/Cookie介绍/">Cookie介绍</a></li><li class="post-list-item"><a class="post-list-link" href="/Programming/Python/Python爬虫实例：从百度图片下载壁纸/">Python爬虫实例：从百度图片下载壁纸</a></li><li class="post-list-item"><a class="post-list-link" href="/Programming/Python/Python线程/">Python线程</a></li><li class="post-list-item"><a class="post-list-link" href="/Programming/Python/XPath与多线程爬虫/">XPath与多线程爬虫</a></li><li class="post-list-item"><a class="post-list-link" href="/Programming/Python/Python之正则表达式/">Python之正则表达式</a></li><li class="post-list-item"><a class="post-list-link" href="/Programming/Python/Python爬虫实例：唐诗三百首/">Python爬虫实例：唐诗三百首</a></li><li class="post-list-item"><a class="post-list-link" href="/Programming/Python/Python爬虫进阶/">Python爬虫进阶</a></li><li class="post-list-item"><a class="post-list-link" href="/Programming/Python/Python初步使用scrapy/">Python初步使用Scrapy</a></li><li class="post-list-item"><a class="post-list-link" href="/Programming/Python/Python开发简单爬虫之实战演练/">Python开发简单爬虫之实战演练</a></li><li class="post-list-item"><a class="post-list-link" href="/Programming/Python/Python开发简单爬虫之爬虫介绍（一）/">Python开发简单爬虫之爬虫介绍</a></li><li class="post-list-item"><a class="post-list-link" href="/Communication/FreeSwitch/FreeSwitch压力测试/">FreeSwitch压力测试</a></li><li class="post-list-item"><a class="post-list-link" href="/Communication/FreeSwitch/FreeSwitch使用mod-xml-curl提供动态用户管理/">FreeSwitch使用mod_xml_curl提供动态用户管理</a></li><li class="post-list-item"><a class="post-list-link" href="/Comprehensive/Tools/Win10下MarkdownPad安装及问题/">Win10下MarkdownPad安装及问题</a></li><li class="post-list-item"><a class="post-list-link" href="/Programming/Linux/Ubuntu14-04安装Apache-php5-mysql-phpmyadmin/">Ubuntu14.04安装Apache+php5+mysql+phpmyadmin</a></li><li class="post-list-item"><a class="post-list-link" href="/Programming/C/Linux下C语言入门准备/">Linux下C语言入门准备</a></li><li class="post-list-item"><a class="post-list-link" href="/Programming/Linux/vi编辑器/">vi编辑器</a></li><li class="post-list-item"><a class="post-list-link" href="/Programming/Shell/Shell简单介绍/">Shell简单介绍</a></li><li class="post-list-item"><a class="post-list-link" href="/Programming/Linux/linux基础（四）/">linux基础（四）</a></li><li class="post-list-item"><a class="post-list-link" href="/Programming/Linux/Linux基础（三）/">Linux基础（三）</a></li><li class="post-list-item"><a class="post-list-link" href="/Programming/Linux/Linux基础（二）/">Linux基础（二）</a></li><li class="post-list-item"><a class="post-list-link" href="/Programming/Linux/Ubuntu Server 14.04 静态IP简单配置/">Ubuntu Server 14.04 静态IP简单配置</a></li><li class="post-list-item"><a class="post-list-link" href="/Programming/Linux/Linux基础（一）/">Linux基础（一）</a></li><li class="post-list-item"><a class="post-list-link" href="/Programming/Android/Android之Handler详解（一）-关联到UI线程/">Android之Handler详解（一）---关联到UI线程</a></li><li class="post-list-item"><a class="post-list-link" href="/Programming/Android/Android之Handler详解（二）-关联到非UI线程/">Android之Handler详解（二）---关联到非UI线程</a></li><li class="post-list-item"><a class="post-list-link" href="/Programming/Android/Android之更新UI的方法/">Android之更新UI的方法</a></li><li class="post-list-item"><a class="post-list-link" href="/Programming/Android/Android之延迟执行某个任务/">Android之延迟执行某个任务</a></li><li class="post-list-item"><a class="post-list-link" href="/Programming/Android/AndroidStudio之环境设置 /">初次安装AndroidStudio之环境设置</a></li><li class="post-list-item"><a class="post-list-link" href="/Programming/Java/java里如何正确计算检验和/">java里如何正确计算检验和</a></li><li class="post-list-item"><a class="post-list-link" href="/Programming/Android/Android之AsyncTask介绍/">Android之AsyncTask介绍</a></li><li class="post-list-item"><a class="post-list-link" href="/Programming/Android/Android之Fragment（一）-简介/">Android之Fragment（一）--简介</a></li><li class="post-list-item"><a class="post-list-link" href="/Programming/Android/Android之Fragment（二）-使用/">Android之Fragment（二）--使用</a></li><li class="post-list-item"><a class="post-list-link" href="/Programming/Android/Android之Fragment（三）-生命周期与回退栈/">Android之Fragment（三）--生命周期与回退栈</a></li><li class="post-list-item"><a class="post-list-link" href="/Programming/Java/Java运行时多态性：继承和接口的实现/">Java运行时多态性：继承和接口的实现</a></li><li class="post-list-item"><a class="post-list-link" href="/Programming/Android/Android之Fragment（四）-Fragment与Activity通讯/">Android之Fragment（四）--Fragment与Activity通讯</a></li><li class="post-list-item"><a class="post-list-link" href="/Programming/Android/Android之RAM、ROM和SD卡/">Android之RAM、ROM和SD卡</a></li><li class="post-list-item"><a class="post-list-link" href="/Programming/Android/Android之使用全局变量的两种方法/">Android之使用全局变量的两种方法</a></li><li class="post-list-item"><a class="post-list-link" href="/Programming/Android/Android之数据存储-SharedPreference/">Android之数据存储--SharedPreference</a></li><li class="post-list-item"><a class="post-list-link" href="/Programming/Android/Android之数据存储-File内部存储/">Android之数据存储--File内部存储</a></li><li class="post-list-item"><a class="post-list-link" href="/Programming/Android/Android之数据存储-外部存储（SD卡）/">Android之数据存储--外部存储（SD卡）</a></li><li class="post-list-item"><a class="post-list-link" href="/Programming/Android/Android之音频播放（MediaPlayer和SoundPool）/">Android之音频播放（MediaPlayer和SoundPool）</a></li><li class="post-list-item"><a class="post-list-link" href="/Programming/Android/Android之话筒、听筒、扬声器/">Android之话筒、听筒、扬声器</a></li><li class="post-list-item"><a class="post-list-link" href="/Programming/Android/Android之数据存数-SQLite数据库/">Android之数据存数--SQLite数据库</a></li><li class="post-list-item"><a class="post-list-link" href="/Programming/Android/Android之AdapterView及子类/">Android之AdapterView及子类</a></li><li class="post-list-item"><a class="post-list-link" href="/Programming/Android/Android之ListView和ListActivity-ArrayAdapter/">Android之ListView和ListActivity--ArrayAdapter</a></li><li class="post-list-item"><a class="post-list-link" href="/Programming/Android/Android之列表视图（LitView）-SimpleAdapter/">Android之列表视图（LitView）--SimpleAdapter</a></li><li class="post-list-item"><a class="post-list-link" href="/Programming/Android/Android之列表视图（LstView）-BaseAdapter/">Android之列表视图（LstView）--BaseAdapter</a></li><li class="post-list-item"><a class="post-list-link" href="/Programming/Android/Android之自动完成文本框（AutoCompleteTextView）/">Android之自动完成文本框（AutoCompleteTextView）</a></li><li class="post-list-item"><a class="post-list-link" href="/Programming/Android/Android之网格视图（GridView）/">Android之网格视图（GridView）</a></li><li class="post-list-item"><a class="post-list-link" href="/Programming/Android/Android之可扩展的列表组件（ExpandableListView）/">Android之可扩展的列表组件（ExpandableListView）</a></li><li class="post-list-item"><a class="post-list-link" href="/Programming/Android/ListView的性能优化之convertView和viewHolder/">ListView的性能优化之convertView和viewHolder</a></li><li class="post-list-item"><a class="post-list-link" href="/Programming/Android/android-layout-weight的真实含义/">android:layout_weight的真实含义</a></li><li class="post-list-item"><a class="post-list-link" href="/Programming/Android/Android之使用9Patch图片作为背景/">Android之使用9Patch图片作为背景</a></li><li class="post-list-item"><a class="post-list-link" href="/Programming/Java/Java综述/">Java综述</a></li><li class="post-list-item"><a class="post-list-link" href="/Programming/Java/Java之数据类型、变量和数组/">Java之数据类型、变量和数组</a></li><li class="post-list-item"><a class="post-list-link" href="/Programming/Java/Java之运算符/">Java之运算符</a></li><li class="post-list-item"><a class="post-list-link" href="/Programming/Java/Java之控制语句/">Java之控制语句</a></li><li class="post-list-item"><a class="post-list-link" href="/Programming/Java/Java之类/">Java之类</a></li><li class="post-list-item"><a class="post-list-link" href="/Programming/Java/Java之方法和类的深入分析/">Java之方法和类的深入分析</a></li><li class="post-list-item"><a class="post-list-link" href="/Programming/Java/Java之继承/">Java之继承</a></li><li class="post-list-item"><a class="post-list-link" href="/Programming/Java/Java之包和接口/">Java之包和接口</a></li><li class="post-list-item"><a class="post-list-link" href="/Programming/Java/Java之异常处理/">Java之异常处理</a></li><li class="post-list-item"><a class="post-list-link" href="/Programming/Java/Java之多线程编程/">Java之多线程编程</a></li><li class="post-list-item"><a class="post-list-link" href="/Programming/Java/Java之枚举、自动装箱与注解（元数据）/">Java之枚举、自动装箱与注解（元数据）</a></li><li class="post-list-item"><a class="post-list-link" href="/Programming/Java/Java之I-O以及其它主题/">Java之I/O以及其它主题</a></li><li class="post-list-item"><a class="post-list-link" href="/Programming/Java/Java之lambda表达式/">Java之lambda表达式</a></li><li class="post-list-item"><a class="post-list-link" href="/Programming/Java/Java之字符串处理/">Java之字符串处理</a></li><li class="post-list-item"><a class="post-list-link" href="/Programming/Java/Java之集合框架/">Java之集合框架</a></li><li class="post-list-item"><a class="post-list-link" href="/Programming/Scala/Scala访问控制修饰符/">Scala访问控制修饰符</a></li><li class="post-list-item"><a class="post-list-link" href="/Programming/Scala/Scala类/">Scala类</a></li><li class="post-list-item"><a class="post-list-link" href="/Programming/Scala/Scala函数（一）/">Scala函数（一）</a></li><li class="post-list-item"><a class="post-list-link" href="/Programming/Scala/Scala函数（二）/">Scala函数（二）</a></li><li class="post-list-item"><a class="post-list-link" href="/Programming/Scala/Scala单例对象/">Scala单例对象</a></li><li class="post-list-item"><a class="post-list-link" href="/Communication/WebRTC/使用WebRTC搭建前端视频聊天室——信令篇/">使用WebRTC搭建前端视频聊天室——信令篇</a></li><li class="post-list-item"><a class="post-list-link" href="/Communication/WebRTC/使用WebRTC搭建前端视频聊天室——数据通道篇/">使用WebRTC搭建前端视频聊天室——数据通道篇</a></li><li class="post-list-item"><a class="post-list-link" href="/Communication/WebRTC/使用WebRTC搭建前端视频聊天室——点对点通信篇/">使用WebRTC搭建前端视频聊天室——点对点通信篇</a></li><li class="post-list-item"><a class="post-list-link" href="/Communication/WebRTC/WebRTC工作流程/">WebRTC工作流程</a></li><li class="post-list-item"><a class="post-list-link" href="/Communication/WebRTC/Android之WebRTC实现/">Android之WebRTC实现</a></li><li class="post-list-item"><a class="post-list-link" href="/Communication/WebRTC/Android之WebRTC介绍（二）/">Android之WebRTC介绍（二）</a></li><li class="post-list-item"><a class="post-list-link" href="/Communication/WebRTC/Android之WebRTC介绍（一）/">Android之WebRTC介绍（一）</a></li><li class="post-list-item"><a class="post-list-link" href="/Communication/WebRTC/WebRTC之turn服务器搭建/">WebRTC之turn服务器搭建</a></li><li class="post-list-item"><a class="post-list-link" href="/Communication/XMPP/openfire之SSL认证/">openfire之SSL认证</a></li><li class="post-list-item"><a class="post-list-link" href="/Communication/XMPP/基于openfire-smack开发Android即时聊天应用-一-用户注册、登陆、修改密码、注销等/">基于openfire+smack开发Android即时聊天应用[一]-用户注册、登陆、修改密码、注销等</a></li><li class="post-list-item"><a class="post-list-link" href="/Communication/OpenSIPs/ubuntu下opensips安装配置/">ubuntu下opensips安装配置</a></li><li class="post-list-item"><a class="post-list-link" href="/Programming/Linux/计算机是如何启动的？/">计算机是如何启动的？</a></li><li class="post-list-item"><a class="post-list-link" href="/Programming/Linux/Linux的启动流程/">Linux的启动流程</a></li><li class="post-list-item"><a class="post-list-link" href="/Communication/XMPP/Smack Message属性扩展---添加自定义元素(标签)/">Smack Message属性扩展--添加自定义元素（标签）</a></li><li class="post-list-item"><a class="post-list-link" href="/Programming/DataStructure/线性表-顺序表/">线性表---顺序表</a></li><li class="post-list-item"><a class="post-list-link" href="/Communication/SIP/SIP对话流程/">SIP对话流程</a></li><li class="post-list-item"><a class="post-list-link" href="/Communication/SIP/SIP服务器类型/">SIP服务器类型</a></li><li class="post-list-item"><a class="post-list-link" href="/Communication/SIP/SIP注册过程/">SIP注册过程</a></li><li class="post-list-item"><a class="post-list-link" href="/Communication/OpenSIPs/openSIPS路由类型/">openSIPS路由类型</a></li><li class="post-list-item"><a class="post-list-link" href="/Communication/OpenSIPs/opensips函数/">opensips函数</a></li><li class="post-list-item"><a class="post-list-link" href="/Communication/SIP/SIP路由字段和机理/">SIP路由字段和机理</a></li><li class="post-list-item"><a class="post-list-link" href="/Communication/OpenSIPs/opensips-csipsimple出现的各种问题/">opensips+csipsimple出现的各种问题</a></li><li class="post-list-item"><a class="post-list-link" href="/Communication/OpenSIPs/opensips介绍/">opensips介绍</a></li><li class="post-list-item"><a class="post-list-link" href="/Programming/Shell/Shell编程（八）-输入输出重定向、文件包含/">Shell编程（八）---输入输出重定向、文件包含</a></li><li class="post-list-item"><a class="post-list-link" href="/Programming/Shell/Shell编程（七）-函数/">Shell编程（七）---函数</a></li><li class="post-list-item"><a class="post-list-link" href="/Programming/Shell/Shell编程（六）-循环/">Shell编程（六）---循环</a></li><li class="post-list-item"><a class="post-list-link" href="/Programming/Shell/Shell编程（五）-if、case/">Shell编程（五）---if、case</a></li><li class="post-list-item"><a class="post-list-link" href="/Programming/Shell/Shell编程（四）-echo、printf/">Shell编程（四）---echo、printf</a></li><li class="post-list-item"><a class="post-list-link" href="/Programming/Shell/Shell编程（三）-字符串、数组/">Shell编程（三）---字符串、数组</a></li><li class="post-list-item"><a class="post-list-link" href="/Programming/Shell/Shell编程（二）-替换、运算符、注释/">Shell编程（二）---替换、运算符、注释</a></li><li class="post-list-item"><a class="post-list-link" href="/Programming/Shell/Shell编程（一）/">Shell编程（一）</a></li><li class="post-list-item"><a class="post-list-link" href="/Communication/WebRTC/使用WebRTC搭建前端视频聊天室——入门篇/">使用WebRTC搭建前端视频聊天室——入门篇</a></li><li class="post-list-item"><a class="post-list-link" href="/Comprehensive/BuildBlog/更改Yelee主题标签云为球形标签云/">更改Yelee主题标签云为球形标签云</a></li><li class="post-list-item"><a class="post-list-link" href="/Communication/FreeSwitch/FreeSwitch安装文档/">FreeSwitch安装文档</a></li><li class="post-list-item"><a class="post-list-link" href="/Comprehensive/BuildBlog/Hexo主题Yelee介绍/">Hexo主题Yelee介绍</a></li><li class="post-list-item"><a class="post-list-link" href="/Programming/Linux/Ubuntu14-04安装JDK与配置环境变量/">Ubuntu14.04安装JDK与配置环境变量</a></li></ul>




    <script>
        
    </script>
</div>
      <footer id="footer">
    <div class="outer">
        <div id="footer-info">
            <div class="footer-left">
                <i class="fa fa-copyright"></i> 
                2016-2017 FreeShow
            </div>
            <div class="footer-right">
                <a href="http://hexo.io/" target="_blank" title="快速、简洁且高效的博客框架">Hexo</a>  Theme <a href="https://github.com/MOxFIVE/hexo-theme-yelee" target="_blank" title="简而不减 Hexo 双栏博客主题  v3.5">Yelee</a> by MOxFIVE <i class="fa fa-heart animated infinite pulse"></i>
            </div>
        </div>
        
            <div class="visit">
                
                    <span id="busuanzi_container_site_pv" style='display:none'>
                        <span id="site-visit" title="本站到访数"><i class="fa fa-user" aria-hidden="true"></i><span id="busuanzi_value_site_uv"></span>
                        </span>
                    </span>
                
                
                    <span>| </span>
                
                
                    <span id="busuanzi_container_page_pv" style='display:none'>
                        <span id="page-visit"  title="本页阅读量"><i class="fa fa-eye animated infinite pulse" aria-hidden="true"></i><span id="busuanzi_value_page_pv"></span>
                        </span>
                    </span>
                
            </div>
        
    </div>
</footer>
    </div>
    
    <script src="/js/GithubRepoWidget.js"></script>

<script data-main="/js/main.js" src="//cdn.bootcss.com/require.js/2.2.0/require.min.js"></script>

    <script>
        $(document).ready(function() {
            var iPad = window.navigator.userAgent.indexOf('iPad');
            if (iPad > -1 || $(".left-col").css("display") === "none") {
                var bgColorList = ["#9db3f4", "#414141", "#e5a859", "#f5dfc6", "#c084a0", "#847e72", "#cd8390", "#996731"];
                var bgColor = Math.ceil(Math.random() * (bgColorList.length - 1));
                $("body").css({"background-color": bgColorList[bgColor], "background-size": "cover"});
            }
            else {
                var backgroundnum = 5;
                var backgroundimg = "url(/background/bg-x.jpg)".replace(/x/gi, Math.ceil(Math.random() * backgroundnum));
                $("body").css({"background": backgroundimg, "background-attachment": "fixed", "background-size": "cover"});
            }
        })
    </script>





    <script type="text/x-mathjax-config">
MathJax.Hub.Config({
    tex2jax: {
        inlineMath: [ ['$','$'], ["\\(","\\)"]  ],
        processEscapes: true,
        skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']
    }
});

MathJax.Hub.Queue(function() {
    var all = MathJax.Hub.getAllJax(), i;
    for(i=0; i < all.length; i += 1) {
        all[i].SourceElement().parentNode.className += ' has-jax';                 
    }       
});
</script>

<script src="//cdn.bootcss.com/mathjax/2.6.1/MathJax.js?config=TeX-AMS-MML_HTMLorMML">
</script>


<div class="scroll" id="scroll">
    <a href="#" title="返回顶部"><i class="fa fa-arrow-up"></i></a>
    <a href="#comments" onclick="load$hide();" title="查看评论"><i class="fa fa-comments-o"></i></a>
    <a href="#footer" title="转到底部"><i class="fa fa-arrow-down"></i></a>
</div>
<script>
    // Open in New Window
    
        var oOpenInNew = {
             github: ".github-widget a", 
            
            
            
            
            
             archives: ".archive-article-title", 
             miniArchives: "a.post-list-link", 
            
             friends: "#js-friends a", 
             socail: ".social a" 
        }
        for (var x in oOpenInNew) {
            $(oOpenInNew[x]).attr("target", "_blank");
        }
    
</script>

<script async src="https://dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js">
</script>
  </div>
</body>
</html>